package cn.zwx.distributed.id.server.controller;

import cn.zwx.distributed.id.server.exception.NoKeyException;
import cn.zwx.distributed.id.server.service.DistributedService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhangwenxue
 */
@RestController
public class DistributedController {

    @Resource
    DistributedService distributedService;


    @RequestMapping(value = "/api/segment/get/{key}")
    public String getSegmentId(@PathVariable("key") String key) {
        if (key == null || key.isEmpty()) {
            throw new NoKeyException();
        }
        return distributedService.getSegmentId(key);
    }

    @RequestMapping(value = "/api/snowflake/get/{key}")
    public String getSnowflakeId(@Validated @PathVariable("key") String key) {
        if (key == null || key.isEmpty()) {
            throw new NoKeyException();
        }
        return distributedService.getSnowflakeId(key);
    }


}
